package com.bryanprogramming.zend.gui;

import com.bryanprogramming.zend.logic.Player;
import com.bryanprogramming.zend.logic.ZEnd;
import com.bryanprogramming.zend.logic.powerups.PowerUp;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;

/**
 *
 * @author charlesbryan
 */
public class UsePowerUpWindow extends JPanel implements PropertyChangeListener {

    private JLabel coinsLabel;
    private Player player;
    private ZEnd game;
    private ArrayList<JButtonWithPrice> availableButtons = new ArrayList<>();

    private UsePowerUpWindow(Player p, ZEnd game) {
        p.addPropertyChangeListener(this);
        this.player = p;
        this.game = game;
        
        buildPanel();
        checkButtonAvailabilty();
    }

    private void buildPanel() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(300, 300));

        JPanel buttonPanel = new JPanel(new GridLayout(player.getNumberOfPowerUps(), 1, 20, 10));

        JButtonWithPrice button;

        for (PowerUp pu : player) {
            button = new JButtonWithPrice(200, pu.getName());
            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    game.usePowerUp(pu);
                    player.removePowerUp(pu);
                    close();
                }
            });
            buttonPanel.add(button);
        }

        this.add(buttonPanel, BorderLayout.CENTER);

        this.add(buildStatusPanel(), BorderLayout.SOUTH);
    }
    
    private void close() {
        JRootPane frame = (JRootPane)this.getParent().getParent();
        JFrame f = (JFrame)frame.getParent();
        f.dispose();
    }

    private JPanel buildStatusPanel() {
        JPanel statusPanel = new JPanel(new BorderLayout());
        statusPanel.setBackground(Color.LIGHT_GRAY);
        statusPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.DARK_GRAY));

        Font font = new Font("San-Serif", Font.BOLD, 10);
        FontMetrics fm = this.getFontMetrics(font);
        int width;
        int height;

        JPanel coinsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        coinsPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.GRAY));
        coinsPanel.setBackground(Color.LIGHT_GRAY);

        width = fm.stringWidth("Gold Coins: 1,999,999");
        height = fm.getHeight();

        coinsLabel = new JLabel("Gold Coins: " + Game.FMT.format(this.player.getGoldCoins()));
        coinsLabel.setFont(font);
        coinsLabel.setBackground(Color.LIGHT_GRAY);
        coinsLabel.setPreferredSize(new Dimension(width, height));
        coinsPanel.add(coinsLabel);
        statusPanel.add(coinsPanel, BorderLayout.WEST);

        return statusPanel;
    }

    public static void createAndShowGUI(Player p, ZEnd game) {
        //Create and set up the window.

        if (p.getNumberOfPowerUps() <= 0) {
            if (JOptionPane.showConfirmDialog(null,
                    "You have no Power Ups to use!\nWould you like to purchase some?", "No Power Ups", JOptionPane.YES_NO_OPTION)
                    == JOptionPane.YES_OPTION) {

                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        BuyPowerUpWindow.createAndShowGUI(p, game);
                    }
                });
                return;
            }
            else {

                return;
            }
        }

        JFrame frame = new JFrame("Z-End");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new UsePowerUpWindow(p, game);
        newContentPane.setOpaque(true); //content panes must be opaque

        //frame.setJMenuBar(((Game) newContentPane).buildMenu());
        frame.setContentPane(newContentPane);
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    private void checkButtonAvailabilty() {
        for (JButtonWithPrice b : availableButtons) {
            b.setEnabled(b.price <= this.player.getGoldCoins());
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(Player.Properties.COINS_AMOUNT_CHANGED)) {
            coinsLabel.setText("Gold Coins: " + Game.FMT.format(this.player.getGoldCoins()));
            checkButtonAvailabilty();
        }
    }

    private class JButtonWithPrice extends JButton {

        int price;

        public JButtonWithPrice(int price, String text) {
            super(text);
            this.price = price;
        }

    }

}
